package repository

import (
	"gluenet/modules/manager/server/adapter/repository/mysql"
	"gluenet/modules/manager/server/domain/dependency"
	"gluenet/modules/manager/server/domain/entity"
	"gluenet/modules/manager/server/infra"
	"gluenet/modules/manager/server/infra/prepare"
	"time"
)

func init() {
	prepare.RegisterRepo(func() {
		repo := NewLinkRepo()
		prepare.InjectDependency(func() dependency.LinksRepo {
			return repo
		})
	})
}

type Link struct {
}

func (l Link) Get(ID string) (obj *entity.Link, e error) {
	//TODO implement me
	panic("implement me")
}

func (l Link) New(lk *entity.Link) (*entity.Link, error) {
	lk.Created = time.Now()
	lk.Updated = time.Now()
	_, err := mysql.DB.InsertOne(lk)
	if err != nil {
		return nil, err
	}
	return lk, err
}

func (l Link) FindBy(query map[string]interface{}) ([]entity.Link, error) {
	var obj []entity.Link

	if err := mysql.DB.Where(query).Find(&obj); err != nil {
		return nil, err
	}

	if len(obj) == 0 {
		return nil, infra.DataNotFind
	}

	return obj, nil
}

func (l Link) Del(ControlGUID, AppGuid string) (obj *entity.Link, e error) {
	//TODO implement me
	panic("implement me")
}

func (l Link) DelAll(ControlGUID string) error {
	var lks = entity.Link{}
	_, err := mysql.DB.Where(`controller_guid = ?`, ControlGUID).Delete(&lks)
	if err != nil {
		return err
	}
	return nil
}

func NewLinkRepo() *Link {
	r := &Link{}
	return r
}
